﻿using GamFi.API.Application.Contracts.Country.GetCountries;
using GamFi.API.Application.Interfaces;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace GamFi.API.Application.Features.Queries.Country.GetCountries;

public class GetCountriesQueryHandler(IDbContext dbContext) : IRequestHandler<GetCountriesQuery, GetCountriesResponse>
{
    public async Task<GetCountriesResponse> Handle(GetCountriesQuery request, CancellationToken cancellationToken)
    {
        var countries = await dbContext.Countries
            .Select(c => new GetCountriesResponseItem
            {
                CountryName = c.Name,
                Code = c.Code,
                AlphaTwo = c.AlphaTwo,
                AlphaThree = c.AlphaThree,
                CountryId = c.Id
            })
            .ToListAsync(cancellationToken);

        return new GetCountriesResponse { Countries = countries };
    }
}